//
//  CommoditySearchViewController.m
//  BusinessApp
//
//  Created by CccDaxIN on 2016/10/31.
//  Copyright © 2016年 macox. All rights reserved.
//

#import "CommoditySearchViewController.h"
#import "CommodityWarehouseTableViewCell.h"
#import "ReleaseCommoditySubModel.h"
#import "ReleaseCommodityViewController.h"
@interface CommoditySearchViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,CommodityWarehouseTableViewCellDelegate>
@property (nonatomic,strong)NSString *commodityName;
@property (nonatomic,strong)UITableView *searchTableview;
@property (nonatomic,strong)NSMutableArray *search_array;
@end

@implementation CommoditySearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品库";
    /**搜索相关
     */
    _commoditySearch.delegate = self;
    _commoditySearch.barTintColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.92 alpha:1.00];
    //设置search图标
     [_commoditySearch setImage:[UIImage imageNamed:@"search-拷贝"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    _search_array = [[NSMutableArray alloc]init];
    [self createCommoditySearchViewTableView];
    /*
     *返回
     */
    UIImage *commoditySearchBackImage = [UIImage imageNamed:@"左箭头"];
    commoditySearchBackImage = [commoditySearchBackImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:commoditySearchBackImage style:UIBarButtonItemStylePlain target:self action:@selector(commoditySearchImageBackAction)];

    
    // Do any additional setup after loading the view from its nib.
}
- (void)commoditySearchImageBackAction{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [NNSingleton sharedSingleton].releasePosition = @"搜索";
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"编辑 ");
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    NSLog(@"返回将要结束编辑");
    return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"点击搜索  %@",searchBar.text);
    _commodityName = [NSString stringWithFormat:@"%@",searchBar.text];
    [self commoditySearchDataRequest];
    [_commoditySearch resignFirstResponder];

}
#pragma -mark --搜索商品库商品接口
- (void)commoditySearchDataRequest{
    [SVProgressHUD showWithStatus:@"正在加载"];
    [GDJHttpTool get:CommoditySearch params:@{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"CategoryId":[NNSingleton sharedSingleton].ERJIID,@"ProductName":_commodityName} success:^(id responseObj) {
        NSLog(@"嫉妒的化身 %@",responseObj);
        //查看错误码
        NSInteger err = [[responseObj valueForKey:@"Err"]integerValue];
        NSLog(@"争取吧%ld",(long) err);
        if (err == 0) {
            [SVProgressHUD dismiss];
            _search_array =   [ReleaseCommoditySubModel mj_objectArrayWithKeyValuesArray:[responseObj objectForKey:@"Products"]];
        }else{
            NSLog(@"%@",responseObj[@"ErrMsg"]);
            [SVProgressHUD showErrorWithStatus:responseObj[@"ErrMsg"]];
            return;
        }
        if (_search_array.count == 0) {
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.center.x - 50, self.view.center.y - 60, 120, 100)];
            image.image = [UIImage imageNamed:@"小哭脸"];
            [self.view addSubview:image];
            
            UILabel *imageLabel = [[UILabel alloc]init];
            imageLabel.frame = CGRectMake(0, image.center.y + 70, kScrernWidth, 30);
            imageLabel.textAlignment = NSTextAlignmentCenter;
            imageLabel.textColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.33 alpha:1.00];
            imageLabel.font = [UIFont systemFontOfSize:18];
            imageLabel.text = @"商品库中没有您要找的商品";
            [self.view addSubview:imageLabel];
        }
        [_searchTableview reloadData];
    }failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark --  创建tableview
- (void)createCommoditySearchViewTableView{
    _searchTableview =[[UITableView alloc]initWithFrame:CGRectMake(0,114,kScrernWidth,kScrernHeight-114) style:UITableViewStylePlain];
    _searchTableview.showsVerticalScrollIndicator = FALSE;
    _searchTableview.delegate=self;
    _searchTableview.dataSource=self;
    _searchTableview.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.94 alpha:1.00];
    _searchTableview.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_searchTableview];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_search_array count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifer=@"commodityCell";
    CommodityWarehouseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CommodityWarehouseTableViewCell" owner:nil options:nil] firstObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    if (_search_array.count != 0) {
        
        ReleaseCommoditySubModel *model = [_search_array objectAtIndex:indexPath.row];
        cell.model = model;
    }
       return cell;
}
#pragma mark -- 代理
- (void)searchButtonActionDelegate:(NSString*)indexPath
{
    [NNSingleton sharedSingleton].commodityLibraryID = [NSString stringWithFormat:@"%@",indexPath];
    [NNSingleton sharedSingleton].releasePosition = @"商品库";
    ReleaseCommodityViewController *releaseCommodity = [[ReleaseCommodityViewController alloc]init];
    [self.navigationController pushViewController:releaseCommodity animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
